<?php
class FriendController extends Zend_Controller_Action
{

    private $user_id;

    //list users
    

    function init () {
        $this->user_id = Zend_Auth::getInstance()->getIdentity()->id;
        
        $init = new Model_Init($this);
        
        //ajax initializatoin
        $ajaxContext = $this->_helper->getHelper('AjaxContext');
        $ajaxContext->addActionContext('getmess', 'html')
            ->addActionContext('modify', 'html')
            ->initContext();
    }

    function onlineAction () {
        $friends = new Model_Friend($this->user_id);
        
        $login = Zend_Auth::getInstance()->getIdentity()->login;
        
        $user_model = new Model_User();
        $user_info = $user_model->getUserInfoByLogin($login);
        
        $this->view->friends = $friends->getFriendsList($user_info->id);
    }

    function indexAction () {
        $friends = new Model_Friend($this->user_id);
        
        $login = $this->_getParam('request');
        
        $user_model = new Model_User();
        $user_info = $user_model->getUserInfoByLogin($login);
        
        $this->view->friends = $friends->getFriendsList($user_info->id);
    }

    function requestlistAction () {
        $friends = new Model_FriendRequest($this->user_id);
        $this->view->friends = $friends->getRequests();
    }

    function addAction () {
        $request_id = $this->_getParam('request');
        
        //approve request id, but check if that erquests truth from that user
        $friend_request = new Model_FriendRequest();
        
        //do that, because in approveRequest() remove that request
        $request_data = $friend_request->getInfoRequestId($request_id)->user_id;
        
        $request = $friend_request->approveRequest($request_id, $this->user_id);
        if ($request) {
            $this->view->message = $this->view->translate(
            "New friend was added");
            
            //add action that user add new friend
            $model_action = new Model_Action();
            $model_action->addAction($this->user_id, 2, $request_data);
        } else {
            $this->view->message = $this->view->translate("Nothing was added");
        }
    }

    function removeAction () {
        $friend_id = $this->_getParam('request');
        $user_id = $this->user_id;
        
        $friend_model = new Model_Friend();
        $friend_model->removeFriends($user_id, $friend_id);
    }

    //send request to other user to add him to friend from current user
    function requestAction () {
        
        //add new row with request
        $request_model = new Model_FriendRequest();
        $user_model = new Model_User();
        $friend_model = new Model_Friend();
        
        $user_request = $user_model->getUserInfoByLogin(
        $this->_getParam('login'))->id;
        $user_id = $this->user_id;
        
        //check if request was send before 
        //or users are friends
        if (! $request_model->checkIfSend($user_id, 
        $user_request) && ! $friend_model->checkIfFriends($user_id, 
        $user_request)) {
            
            $request_model->user_id = $user_id;
            $request_model->user_request = $user_request;
            $request_model->save();
            
            $this->view->message = $this->view->translate(
            "Good idea to add that friend");
        } else {
            $this->view->message = $this->view->translate(
            "Be patient! Wait for approving your request");
        }
    
    }
    //show years of visit
     public function visitAction () {
         
     }

     
    //show users from year was on kazantip
    public function yearAction () {
        $year = $this->_getParam('request');
        $year = 'visit_' . $year;
        $page = 1;
        $year_numb = $this->_getParam('request');
        
        if($year_numb == 1000){
            $model_visit = new Model_User();
            $this->view->friends = $model_visit->getAllUsers($page);
        }else{
            $model_visit = new Model_Visits($year);
            $this->view->friends = $model_visit->getUsersByYear($page);
        }
        
        
        //add next page for template
        $this->view->page = 1;
        $this->view->year = $this->_getParam('request');
    
    }
    
    
    public function getmessAction () {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        
        /******* ATTENTION *******/
        /************ DANGEROUS ************/
        //SO STUPID CODE!!!!!!!!!! DON'T LOOK AT IT!!!
        //TODO do something a-a-a--a- ;'=(
        //I'm so sorry for that code(((
        
        $year = $this->_getParam('request');
        $year = 'visit_' . $year;
        
        $year_numb = $this->_getParam('request');
        $page = $this->_getParam('page');
        
        $page ++;
        if($year_numb == 1000){
            $model_visit = new Model_User();
            $friends = $model_visit->getAllUsers($page);
        }else{
            $model_visit = new Model_Visits($year);
            $friends = $model_visit->getUsersByYear($page);
        }
        
        //add next page for template 
        
        $response = "";
        
      foreach ($friends as $friend){
 	 $response .= '<div id="'.$page.'" class="message_box" 
 	
 	style="
width: 120px;
height: 165px; 
float: left; margin: 20px; 
text-align: center;

">';
    	
        $cur_user_id = Zend_Auth::getInstance()->getIdentity()->id;
        if($year_numb == 1000){
             $user_id_model = $friend->id;
         }else{
            	$user_id_model = $friend->user_id;
         }
         
    	$model_user = new Model_User($user_id_model);
    	
            	$response .= '<div style="
                    height: 125px; 
                    ">
            	<a
            	href="'.$this->view->url(array("controller" => $model_user->login, "action" => null, "request"=>null),"default").'">';
                		if ($model_user->avatar == null){ 
                			$response .= '<img src="'.$this->view->baseUrl("img" . DIRECTORY_SEPARATOR . $model_user->gender).'sex_ava.png"   width="120" alt="avatar image">'; 
                		}else{ 
                			$response .= '<img src="'.$this->view->baseUrl("upload" . DIRECTORY_SEPARATOR ."thumb". DIRECTORY_SEPARATOR . $model_user->avatar).'" width="120" alt="avatar image">';
                		}
                $response .= '</a>'; 
                
            	$response .= '</div>
                <a 
            	href="'.$this->view->url(array("controller" => $model_user->login, "action" => null, "request"=>null),"default").'">';
                $response .= $model_user->login;
                $response .= '</a> 
                <br/>';
                
                
                
                    if($model_user->checkOnline($user_id_model)){
                	$response .= $this->view->translate("Online");
                		}else{
                	$response .= $this->view->translate("Offline");
                		}
    	
    	
        $response .= '<br/>
     </div>';
     
     }
     
    $response .= '<div id="last_msg_loader"></div>'; 
    echo $response;
    
    }
    
    
}









